package com.example.demo.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.pojo.Order.Orders;
import com.example.demo.pojo.Order.PutOrders;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

@Component
public class OrderUtils {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 发送订单信息到kafka时的内容
     *
     * @param order        订单pojo类
     * @param customStatus 订单状态（未成交或已成交）
     * @return 消息体
     */
    public String createOrderMessage(PutOrders order, String customStatus) {
        return String.format(
                "{"
                        + "\"orderId\": \"%s\", "
                        + "\"userId\": \"%d\", "
                        + "\"stockCode\": \"%s\", "
                        + "\"orderType\": \"%s\", "
                        + "\"price\": %.2f, "
                        + "\"quantity\": \"%d\", "
                        + "\"status\": \"%s\", "
                        + "\"time\": \"%s\""
                        + "}",
                order.getOrderId(),
                order.getUserId(),
                order.getStockCode(),
                order.getOrderType(),
                order.getPrice(),
                order.getQuantity(),
                customStatus,
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(order.getTime())
        );


    }

    /**
     * 发送订单信息到kafka时的内容
     *
     * @param order        订单pojo类
     * @param customStatus 订单状态（未成交或已成交）
     * @return 消息体
     */
    public String createOrderMessageNew(Orders order, String customStatus) {
        return String.format(
                "{"
                        + "\"orderId\": \"%s\", "
                        + "\"userId\": \"%d\", "
                        + "\"stockCode\": \"%s\", "
                        + "\"orderType\": \"%s\", "
                        + "\"price\": %.2f, "
                        + "\"quantity\": \"%d\", "
                        + "\"status\": \"%s\", "
                        + "\"time\": \"%s\""
                        + "}",
                order.getOrderId(),
                order.getUserId(),
                order.getStockCode(),
                order.getOrderType(),
                order.getPrice(),
                order.getOrderQuantity(),
                customStatus,
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(order.getTime())
        );


    }

    public Orders getOrder(Integer id, Integer userId, String stockCode, String orderType) {
        // 根据条件查询订单信息
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper
                .eq("order_id", id)
                .eq("user_id", userId)
                .eq("stock_code", stockCode)
                .eq("order_type", orderType)
                .eq("status", "买入");
        return orderMapper.selectOne(ordersQueryWrapper);
    }

    public Orders hasUserBoughtStock(
            Integer id,
            Integer userId,
            String stockCode,
            String orderType,
            String stockName) {
        // 创建查询条件
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper
                .eq("order_id", id)
                .eq("user_id", userId)
                .eq("stock_code", stockCode)
                .eq("order_type", orderType)
                .eq("status", "买入")
                .eq("stock_name", stockName);

        // 查询订单
        return orderMapper.selectOne(ordersQueryWrapper); // 如果找到订单则表示用户购买过此股票，返回true，否则返回false
    }

}
